import time

from django.db import models
from django.db.models import Sum, F

from api.utils.enums import OrderStatus


class Order(models.Model):
    order_num = models.IntegerField("订单号", default=0)
    user = models.ForeignKey("User", on_delete=models.CASCADE, related_name="orders")
    address = models.ForeignKey("ShipAddress", on_delete=models.CASCADE, related_name="orders")
    status = models.CharField(choices=OrderStatus.to_choices(), max_length=256)
    cancel = models.BooleanField("是否取消订单", default=False)
    total_rmb = models.PositiveIntegerField("总人民币", default=0)
    total_point = models.PositiveIntegerField("总积分", default=0)


class OrderStatusChange(models.Model):
    order = models.ForeignKey(
        "Order", on_delete=models.CASCADE, related_name="order_status_changes"
    )
    old_status = models.CharField("老状态", choices=OrderStatus.to_choices(), max_length=256)
    new_status = models.CharField("新状态", choices=OrderStatus.to_choices(), max_length=256)
    data_created = models.DateTimeField(auto_now_add=True, db_index=True)


class OrderLine(models.Model):
    order = models.ForeignKey("Order", on_delete=models.CASCADE, related_name="order_lines")
    prodcut = models.ForeignKey("Porduct", on_delete=models.CASCADE, related_name="order_lines")
    title = models.CharField("商品title", default="", max_length=253)
    quantity = models.PositiveIntegerField("数量", default=0)
    pirce = models.PositiveIntegerField("价格", default=0)
